//
//  LLStatusOriginalView.swift
//  LLWeibo09
//
//  Created by 林 on 16/4/2.
//  Copyright © 2016年 lin. All rights reserved.
//

import UIKit

/// 原创微博的view
class LLStatusOriginalView: UIView {
    
    // MARK: - 属性
    /// 微博模型
    var status: LLStatus? {
        didSet {
            // 当别人来设置微博模型的时候,我们根据微博模型来设置原创微博的相关内容
            /// 1.设置头像
            if let imageUrl = status?.user?.profile_image_url {
                let url = NSURL(string: imageUrl)
                iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar"))
            }
            
            /// 设置名称
            nameLabel.text = status?.user?.screen_name
            
            /// 设置时间是
            timeLabel.text = status?.created_at
            
            /// 来源
            sourceLabel.text = status?.source
            
            /// 会员等级
            if let mbrank = status?.user?.mbrank {
                if mbrank > 0 && mbrank <= 6 {
                    memberView.image = UIImage(named: "common_icon_membership_level\(mbrank)")
                } else { // 防止cell复用,没有会员的就不要等级
                    memberView.image = nil
                }
            }
            
            /// 认证图标
            if let verified_type = status?.user?.verified_type {
                var image: UIImage?
                
                switch verified_type {
                case 0:
                    image = UIImage(named: "avatar_vip")
                case 2,3,5:
                    image = UIImage(named: "avatar_enterprise_vip")
                case 220:
                    image = UIImage(named: "avatar_grassroot")
                default:
                    image = nil
                }
                
                verifiedView.image = image
            }
            
            /// 设置微博内容
            contentLabel.text = status?.text
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor.whiteColor()
        prepareUI()
    }

    private func prepareUI() {
        // 添加子控件 
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(memberView)
        addSubview(verifiedView)
        addSubview(contentLabel)
        
        // 添加约束
        /// 用户头像
        iconView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(LLStatusCellMargin)
            make.top.equalTo(self).offset(LLStatusCellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        /// 用户名称
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView.snp_trailing).offset(LLStatusCellMargin)
            make.top.equalTo(iconView)
        }
        
        /// 时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView.snp_trailing).offset(LLStatusCellMargin)
            make.bottom.equalTo(iconView)
        }
        
        /// 来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(LLStatusCellMargin)
            make.centerY.equalTo(timeLabel)
        }
        
        /// 会员等级
        memberView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel.snp_trailing).offset(LLStatusCellMargin)
            make.centerY.equalTo(nameLabel)
            make.size.equalTo(CGSize(width: 14, height: 14))
        }
        
        /// 认证图标
        verifiedView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_trailing)
            make.centerY.equalTo(iconView.snp_bottom)
            make.size.equalTo(CGSize(width: 17, height: 17))
        }
        
        /// 微博内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_bottom).offset(LLStatusCellMargin)
            make.leading.equalTo(self).offset(LLStatusCellMargin)
            make.trailing.equalTo(self).offset(-LLStatusCellMargin)
        }
        
        /// 原创cell的底部参照contentLabel的底部
        self.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(contentLabel).offset(LLStatusCellMargin)
        }

    }
    
    // MARK: - 懒加载 
    /// 用户头像
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar"))
    
    /// 用户名称
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(LLStatusCellContentSize)
        
        return label
    }()
    
    /// 时间
    private lazy var timeLabel: UILabel = {
        let label = UILabel()
        
        label.textColor = UIColor.orangeColor()
        label.font = UIFont.systemFontOfSize(9)
        
        return label
        
    }()
    
    /// 来源
    private lazy var sourceLabel: UILabel = {
        let label = UILabel()
        
        label.textColor = UIColor.lightGrayColor()
        label.font = UIFont.systemFontOfSize(9)
        
        return label
    }()

    /// 会员等级
    private lazy var memberView: UIImageView = UIImageView()
    
    /// 认证图标
    private lazy var verifiedView: UIImageView = UIImageView()
    
    /// 原创微博内容
    private lazy var contentLabel: UILabel = {
        let label = UILabel()
        
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(LLStatusCellContentSize)
        label.numberOfLines = 0
        
        return label
    }()

    
}
